package cz.fi.muni.pb138.spring2013.dtd2xsd.entities;

import cz.fi.muni.pb138.spring2013.dtd2xsd.visitor.interfaces.ContentRestriction;
import cz.fi.muni.pb138.spring2013.dtd2xsd.visitor.interfaces.ExtendedContentRestriction;
import cz.fi.muni.pb138.spring2013.dtd2xsd.visitor.interfaces.Visitor;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * This class represents an element in DTD and XML Schema
 *
 * @author Slavomir Krupa
 * @author Valdemar Svabensky
 */
public class ElementRestriction implements ExtendedContentRestriction{

    private String name;
    private ContentRestriction content;
    private List<Attribute> attributes = new ArrayList<>();
    private boolean required;
    private boolean multiple;

    public ElementRestriction() {
        this.name = null;
        this.content = null;
        this.required = true;
        this.multiple = false;
    }

    @Override
    public void accept(Visitor visitor) {
        visitor.visit(this);
        for (Attribute attribute : attributes) {
            attribute.accept(visitor);
        }
        content.accept(visitor);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public ContentRestriction getContent() {
        return content;
    }

    public void setContent(ContentRestriction content) {
        this.content = content;
    }

    public List<Attribute> getAttributes() {
        return Collections.unmodifiableList(attributes);
    }

    public void addAttribute(Attribute attribute) {
        this.attributes.add(attribute);
    }

    @Override
    public boolean isRequired() {
        return required;
    }

    @Override
    public void setRequired(boolean required) {
        this.required = required;
    }

    @Override
    public boolean isMultiple() {
        return multiple;
    }

    @Override
    public void setMultiple(boolean multiple) {
        this.multiple = multiple;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 97 * hash + (this.name != null ? this.name.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final ElementRestriction other = (ElementRestriction) obj;
        if ((this.name == null) ? (other.name != null) : !this.name.equals(other.name)) {
            return false;
        }
        if (this.content != other.content && (this.content == null || !this.content.equals(other.content))) {
            return false;
        }
        if (this.attributes != other.attributes && (this.attributes == null || !this.attributes.equals(other.attributes))) {
            return false;
        }
        if (this.required != other.required) {
            return false;
        }
        if (this.multiple != other.multiple) {
            return false;
        }
        return true;
    }
}
